<?php

class BgimageController extends Yaf_Controller_Abstract
{
    private $bgimageModel;
    private $sessoin;
    private $imageProcess;
    public function init()
    {
        $this->bgimageModel = new BgimageModel();
        $this->session = Yaf_Session::getInstance();
        $this->imageProcess = new ImageProcessor();
    }
    public function indexAction()
    {
        $menuid = $_GET['menu_id'];
        $menuid = intval($menuid);
        if ($menuid==0)
            $menuid = 2;
        $bgimage = $this->bgimageModel->fetchBgimageByMenuid($menuid);
        $this->getView()->assign('bgimage',$bgimage);
        
        $menu_info = $this->bgimageModel->fetchMenu();
        $this->getView()->assign('menu',$menu_info);
        $this->getView()->assign('menu_id',$menuid);
    }
    public function createAction()
    {
        $menu_info = $this->bgimageModel->fetchMenu();
        $this->getView()->assign('menu',$menu_info);
    }
    public function modifyAction()
    {
        $career = array();
        $id = intval($this->getRequest()->getParam('id'));
        if ($id)
        {
            $career = $this->bgimageModel->fetchCareerById($id);
            $career[0]['job_describe'] = explode('~!~', $career[0]['job_describe']);
            $career[0]['job_requirement'] = explode('~!~', $career[0]['job_requirement']);
        }
        $this->getView()->assign('career',$career[0]);
    }
   
    public function insertAction()
    { 
        $menu_id = intval($this->getRequest()->getPost('menu_id'));
        if ($menu_id!=0)
        {
            $bgImage = $this->imageProcess->upload_multi_image('bg_image','bgImage');
            for($i=0;$i<count($bgImage);$i++)
            {
                $data = array(
                    'menu_id' => $menu_id,
                    'filename' => $bgImage[$i]
                );
                $this->bgimageModel->insertBgimage($data);
            }
        }
        $this->forward('index');
        return FALSE;
    }
    
    public function updateAction()
    {
        $id = $this->getRequest()->getPost('id');
        $title = $this->getRequest()->getPost('title');
        $department = $this->getRequest()->getPost('department');
        $superior = $this->getRequest()->getPost('superior');
        $subordinate = $this->getRequest()->getPost('subordinate');
        $describe = $this->getRequest()->getPost('describe');
        $requirement = $this->getRequest()->getPost('requirement');
        $describe = implode('~!~', $describe);
        $requirement = implode('~!~', $requirement);
        $data = array(
            'title' => $title,
            'department' => $department,
            'superior' => $superior,
            'subordinate' => $subordinate,
            'job_describe' => $describe,
            'job_requirement' => $requirement
        );
        $last_id = $this->careerModel->updateCareer($data,array('id'=>$id));
        $this->forward('index');
        return FALSE;
    }
    public function deleteAction()
    {
        $id = intval($this->getRequest()->getParam('id'));
        if ($id)
        {
            $bgimage = $this->bgimageModel->fetchBgimageById($id);
            unlink('../bg_image/'.$bgimage[0]['filename']);
            $this->bgimageModel->delete('bg_image',array('id'=>$id));
        }
        $this->forward('index');
        return FALSE;
    }
    
    public function bgimageOrderAction()
    {
        $order = $this->getRequest()->getPost('order');
        $menuid = $this->getRequest()->getPost('menu_id');
        if ($menuid!='')
        {
            $bgimage = $this->bgimageModel->fetchBgimageByMenuid($menuid);
            for($i=0;$i<count($bgimage);$i++)
            {
                if ($order[$i])
                    $this->bgimageModel->updateBgimage(array('order_id'=>$order[$i]),array('id'=>$bgimage[$i]['id']));
            }
        }
        $this->forward('index');
        return FALSE;
    }
}
?>
